package com.et.machine.manager.vm.test

import android.util.Log
import androidx.lifecycle.ViewModel
import com.et.drive.out.IOutGoodsImpl
import com.et.drive.out.OutGoodsParam
import com.et.machine.BuildConfig
import com.et.machine.convert.OutGoodsStatusEnum
import com.et.machine.convert.machineIdInfo
import com.et.machine.convert.machineInfo
import com.et.util.toByteArray
import kotlin.math.ceil
import kotlin.math.max
import kotlin.random.Random

class ChannelTestViewModel : ViewModel() {


    private val foodNumber =
        (machineInfo?.nFood!!) +
                (machineIdInfo?.nFood2!!) +
                (machineIdInfo?.nFood3!!) +
                (machineIdInfo?.nFood4!!)


    fun addBoxList(): MutableList<ChannelTestFragment.Companion.Box> {
        val boxData = mutableListOf<ChannelTestFragment.Companion.Box>()
        Log.d(
            "MachineIdInfo", "nFood:${machineInfo?.nFood!!}, " +
                    "nFood2:${(machineIdInfo?.nFood2!!)}, " +
                    "nFood3:${(machineIdInfo?.nFood3!!)}, " +
                    "nFood4:${(machineIdInfo?.nFood4!!)}"
        )

        if (machineInfo?.cDrink!! > 0) {
            boxData.add(ChannelTestFragment.Companion.Box(false, ChannelTestFragment.BOX_11))
        }
        for (i in 9 downTo 10 - ceil(foodNumber / 48.0).toInt()) { // (foodNumber + 47) / 48
            boxData.add(ChannelTestFragment.Companion.Box(false, i))
        }
        for (i in 1..(machineInfo?.cBox!!)) {
            boxData.add(ChannelTestFragment.Companion.Box(false, i))
        }

        return boxData
    }

    /**
     * notify channel count
     */
    fun notifyChannelInfo(box: Int): Int {

        return when (box) {
            11 -> {
                machineInfo?.nDrink!!
            }
            9, 8, 7, 6 -> {
                max(48.coerceAtMost(foodNumber - (9 - box) * 48), 0)
            }
            1, 2, 3, 4, 5 -> {
                40
            }
            else -> 0
        }
    }


    fun outGoods(box: Int, channel: Int): OutGoodsStatusEnum {
        val p = OutGoodsParam()
        when (BuildConfig.FLAVOR) {
            "et" -> {
                val serialByteArray = Random.nextInt().toByteArray(3)
                val data = byteArrayOf(
                    0, 0,
                    box.toByte(), 1, channel.toByte(),
                    0, 0, 0, 0x50,
                    serialByteArray[0], serialByteArray[1], serialByteArray[2],
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    0
                )
                p.byteArray = data
            }
            "dc" -> {

            }
            "fs" -> {
            }
        }
        return IOutGoodsImpl.outGoods(p)
    }


    fun box2Name(id: Int): String {
        return when (id) {
            ChannelTestFragment.BOX_11 -> "$id 饮料机"
            ChannelTestFragment.BOX_06, ChannelTestFragment.BOX_07, ChannelTestFragment.BOX_08, ChannelTestFragment.BOX_09 -> "$id 食品机"
            ChannelTestFragment.BOX_01, ChannelTestFragment.BOX_02, ChannelTestFragment.BOX_03, ChannelTestFragment.BOX_04, ChannelTestFragment.BOX_05 -> "$id 格子柜"
            else -> "unknown"
        }
    }


}